function [] = part1()
    
    d1 = 362;
    d2 = -831;
    
    tc1 = dec22compl(d1);
    tc2 = dec22compl(d2);
    tc1
    tc2
    
    be1 = booth_encoding(tc1);
    be2 = booth_encoding(tc2);
    
    be1
    be2
end

function Y = booth_encoding(X)
    p_msb_pos = length(X);
    i = 1;
    while(p_msb_pos > 1)
        if(i == 1)
            p = [X(p_msb_pos-1:p_msb_pos) 0];
            p_msb_pos = p_msb_pos-1;
        elseif(p_msb_pos == 2)
            % sign extend, unsigned
            p = [X(1) X(p_msb_pos-1:p_msb_pos)];
            p_msb_pos = p_msb_pos-1;
        else
            p = X(p_msb_pos-2:p_msb_pos);
            p_msb_pos = p_msb_pos-2;
        end
        Y(i) = encode(p);
        i=i+1;
    end
    Y = fliplr(Y);
end

function [Y] = encode(X)
    Y = 0;
    if    (equal(X,[0 0 0]))
        Y = 0;
    elseif(equal(X,[0 0 1]))
        Y = 1;
    elseif(equal(X,[0 1 0]))
        Y = 1;
    elseif(equal(X,[0 1 1]))
        Y = 2;
    elseif(equal(X,[1 0 0]))
        Y = -2;
    elseif(equal(X,[1 0 1]))
        Y = -1;
    elseif(equal(X,[1 1 0]))
        Y = -1;
    elseif(equal(X,[1 1 1]))
        Y = 0;
    else
        %error
    end    
end

function [t] = equal(A,B)
    t = sum(A == B) == 3;
end